<?php

/**
 * 前台 在线留言 模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: comment.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class commentAction extends Action
{

    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('comment');
        $this->M('index')->getCommon();
    }

    public function index()
    {
        $this->V('comment');
    }

    public function newslist()
    {
        $this->news_model->getArticleList(PHP_SELF . '?m=download.newslist&page=', 'download');
        $this->V('list');
    }

    public function save()
    {
        if (empty($_POST) || count($_POST) < 6) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $comment_title = trim($this->H($this->getParam('comment_title')));
        $comment_author = trim($this->H($this->getParam('comment_author')));
        $comment_author_tel = trim($this->H($this->getParam('comment_author_tel')));
        $comment_company = trim($this->H($this->getParam('comment_company')));
        $comment_address = trim($this->H($this->getParam('comment_address')));
        $comment_author_email = trim($this->H($this->getParam('comment_author_email')));
        $comment_content = trim($this->H($this->getParam('comment_content')));
        $yzm = intval($this->getParam('yzm'));

        if (trim(md5($yzm)) != trim($_SESSION['comment_valid'])) {
            $this->redirect("效验码输入错误!", PHP_SELF . "?m=comment");
            exit();
        }
        /* 验证不能为空 */
        if (empty($comment_title)) {
            $this->redirect('评论标题不能为空');
            exit;
        }
        if (empty($comment_author)) {
            $this->redirect('评论作者不能为空');
            exit;
        }
        if (!isset($comment_author_tel)) {
            $this->redirect('电话不能为空!');
            exit;
        } else {
            if (!regExp::check_mobile($comment_author_tel)) {
                $this->redirect('手机号码格式不正确!');
                exit;
            }
        }
        if (!isset($comment_author_email)) {
            $this->redirect('email不能为空!');
            exit;
        } else {
            if (!regExp::Email($comment_author_email)) {
                $this->redirect('email格式不正确!');
                exit;
            }
        }
        if (!isset($comment_content)) {
            $this->redirect('评论内容不能为空!');
            exit;
        }
        if (empty($yzm)) {
            $this->redirect('验证码不能为空!');
            exit;
        }
        // TODO goon to verify

        $info = array(
            'comment_title' => $comment_title,
            'comment_author' => $comment_author,
            'comment_author_tel' => $comment_author_tel,
            'comment_company' => $comment_company,
            'comment_address' => $comment_address,
            'comment_author_email' => $comment_author_email,
            'comment_content' => $comment_content,
            'comment_time' => time(),
            'comment_author_ip' => $_SERVER["REMOTE_ADDR"]
        );

        //insert save article_class
        $rs = $this->tmp_model->addcomment($info);
        if ($rs) {
            $this->redirect('留言成功', PHP_SELF . '?m=comment');
        } else {
            $this->redirect('留言失败');
        }
    }

}